package com.catmiw.stock.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.catmiw.funds.tool.DateTool;
import com.catmiw.stock.dao.IncomingDetailMapper;
import com.catmiw.stock.dao.IncomingMapper;
import com.catmiw.stock.service.SessionFactory;
import com.catmiw.stock.domain.Incoming;

public class IncomingServiceImpl implements IncomingService {
	public int insertAll(List<Incoming> ins) {
		SqlSession session=SessionFactory.getInstance().getSession();
		IncomingMapper cm=session.getMapper(IncomingMapper.class);
		int count=0;
		
		for (Incoming icm:ins){
			Incoming old=cm.select(icm);
			if (old==null){
				System.out.println(icm.getCode()+"\t"+
						DateTool.dateToString(icm.getDate(),"yyyy-MM-dd")+
						"\t"+icm.getKind()+"\t"+icm.getLabel());
				cm.insert(icm);
				count++;
			}
		}
		session.commit();
		session.close();
		
		return count;
	}

	public int insertAllDetail(List<Incoming> ins) {
		SqlSession session=SessionFactory.getInstance().getSession();
		IncomingDetailMapper cm=session.getMapper(IncomingDetailMapper.class);
		int count=0;
		
		for (Incoming icm:ins){
			Incoming old=cm.select(icm);
			if (old==null){
				System.out.println(icm.getCode()+"\t"+
						DateTool.dateToString(icm.getDate(),"yyyy-MM-dd")+
						"\t"+icm.getKind()+"\t"+icm.getLabel());
				cm.insert(icm);
				count++;
			}
			count++;
		}
		session.commit();
		session.close();
		
		return count;
	}

	public void deleteAll() {
		SqlSession session=SessionFactory.getInstance().getSession();
		IncomingMapper cm=session.getMapper(IncomingMapper.class);
		cm.deleteAll();
		session.commit();
		session.close();
	}

	public void deleteAllDetail() {
		SqlSession session=SessionFactory.getInstance().getSession();
		IncomingDetailMapper cm=session.getMapper(IncomingDetailMapper.class);
		cm.deleteAll();
		session.commit();
		session.close();
	}

}
